package com.baomidou.springwind.excel;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.NetworkDelivery;
import com.baomidou.springwind.entity.NetworkDepot;
import com.baomidou.springwind.entity.WeekOrder;
import com.baomidou.springwind.entity.NetworkCandidate;
import com.baomidou.springwind.service.NetworkCandidateService;
import com.baomidou.springwind.service.NetworkDepotService;
//import com.sun.xml.internal.ws.api.pipe.FiberContextSwitchInterceptor;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author luox
 */
@Component
public class NetworkCandidateDataReader extends ExcelReader<NetworkDepot> {

    @Autowired
    private NetworkCandidateService networkCandidateService;
    @Autowired
    private NetworkDepotService networkDepotService;
    /**
     * 保存数据
     * 处理的有4类数据
     * 1：depot表的仓库现状数据
     * 2：depot表的仓库新增节点数据
     * 3：Candidate表的仓库现状数据
     * 4：Candidate表的仓库未来数据
     * @param dataList
     * @param batchId
     * @return
     */
    @Override
    protected String saveData(List<NetworkDepot> dataList, Long batchId) {
        List<NetworkDepot> depots1 = new ArrayList<NetworkDepot>();
        List<NetworkCandidate> candidates = new ArrayList<NetworkCandidate>();
        EntityWrapper<NetworkCandidate> wrapper = new EntityWrapper<NetworkCandidate>();
        wrapper.eq("batch_id", batchId);
        deleteFlowAnalysisFile(batchId);
        networkCandidateService.delete(wrapper);//删除之前导入的数据
        EntityWrapper<NetworkDepot> wrapper1 = new EntityWrapper<NetworkDepot>();
        wrapper1.eq("batch_id",batchId);
        List<NetworkDepot> depots2 = networkDepotService.selectList(wrapper1);
        wrapper1.eq("type",1);
        networkDepotService.delete(wrapper1);
        NetworkCandidate candidate;
        for (NetworkDepot depot : dataList) {
            depot.setBatchId(batchId);
            depot.setCreateTime(new Date());
            depot.setOperatorId(getCurrentUserId());
            if(depot.getType()==1){
                depots1.add(depot);
                candidate = new NetworkCandidate();
                candidate.setBatchId(batchId);
                candidate.setCandidatePoint(depot.getDepotName());
                candidate.setType(depot.getType());
                candidates.add(candidate);
            }
            if(depot.getType()==0){
                for(int i=0;i<depots2.size();i++){
                    if(depot.getDepotName().equals(depots2.get(i).getDepotName())){
                        depots2.get(i).setDepotCapacity(depot.getDepotCapacity());
                        depots2.get(i).setWorkerCost(depot.getWorkerCost());
                        depots2.get(i).setDepotRent(depot.getDepotRent());
                        depots2.get(i).setDeviceCost(depot.getDeviceCost());
                        depots2.get(i).setEnergyCost(depot.getEnergyCost());
                        depots2.get(i).setOtherCost(depot.getOtherCost());
                    }
                }
            }
        }
        networkDepotService.updateBatchById(depots2);
        networkDepotService.insertBatch(depots1,100);
        networkCandidateService.insertBatch(candidates, 100);
        return "";
    }

    private void deleteFlowAnalysisFile(Long batchId) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String path = request.getSession().getServletContext().getRealPath("");
        path = path.substring(0, path.lastIndexOf("\\"));
        path = path.substring(0, path.lastIndexOf("\\"));
        File file = new File(path + "/temp/route/" + batchId + ".obj");
        file.delete();
    }

    @Override
    protected List<NetworkDepot> verifyData(HSSFWorkbook workbook) {
        List<NetworkDepot> weekOrders = new ArrayList<NetworkDepot>();
        List<NetworkDepot> depots1 = dataNow(workbook);
        List<NetworkDepot> depots2 =dataNew(workbook);
        weekOrders.addAll(depots1);
        weekOrders.addAll(depots2);
        return weekOrders;
    }

    /**
     * excel2003现在的仓库的有关数据
     * @param workbook
     * @return
     */
    private List<NetworkDepot> dataNow(HSSFWorkbook workbook){
        List<NetworkCandidate> weekOrders = new ArrayList<NetworkCandidate>();
        List<NetworkDepot> depots = new ArrayList<NetworkDepot>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        NetworkCandidate candidate;
        NetworkDepot depot;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            /*candidate = new NetworkCandidate();
            candidate.setCandidatePoint(getStringCellValue(sheet.getRow(i), 0));
            candidate.setType(getIntCellValue(sheet.getRow(i), 1));
            candidate.setType1(0);
            weekOrders.add(candidate);*/
            depot = new NetworkDepot();
            depot.setDepotName(getStringCellValue(sheet.getRow(i),0));
            depot.setDepotLevel(getIntCellValue(sheet.getRow(i),1));
            depot.setDepotLng(getStringCellValue(sheet.getRow(i),2));
            depot.setDepotLat(getStringCellValue(sheet.getRow(i),3));
            depot.setDepotCapacity(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),4))));
            depot.setDepotRent(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),5))));
            depot.setWorkerCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),6))));
            depot.setDeviceCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),7))));
            depot.setEnergyCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),8))));
            depot.setOtherCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),9))));
            depot.setType(0);
            depots.add(depot);
        }
        return depots;
    }

    /**
     * excel2003优化节点的相关数据
     * @param workbook
     * @return
     */
    private List<NetworkDepot> dataNew(HSSFWorkbook workbook){
        List<NetworkCandidate> weekOrders = new ArrayList<NetworkCandidate>();
        List<NetworkDepot> depots = new ArrayList<NetworkDepot>();
        HSSFSheet sheet = workbook.getSheetAt(1);
        int lastRowNum = sheet.getLastRowNum();
        NetworkCandidate candidate;
        NetworkDepot depot;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            /*candidate = new NetworkCandidate();
            candidate.setCandidatePoint(getStringCellValue(sheet.getRow(i), 0));
            candidate.setType(getIntCellValue(sheet.getRow(i), 1));
            candidate.setType1(1);
            weekOrders.add(candidate);*/
            depot = new NetworkDepot();
            depot.setDepotName(getStringCellValue(sheet.getRow(i),0));
            depot.setDepotLevel(getIntCellValue(sheet.getRow(i),1));
            depot.setDepotLng(getStringCellValue(sheet.getRow(i),2));
            depot.setDepotLat(getStringCellValue(sheet.getRow(i),3));
            depot.setDepotCapacity(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),4))));
            depot.setDepotRent(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),5))));
            depot.setWorkerCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),6))));
            depot.setDeviceCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),7))));
            depot.setEnergyCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),8))));
            depot.setOtherCost(BigDecimal.valueOf(Double.valueOf(getStringCellValue(sheet.getRow(i),9))));
            depot.setType(1);
            depots.add(depot);
        }
        return depots;
    }


    @Override
    protected List<NetworkDepot> verifyData7(XSSFWorkbook workbook) {
        List<NetworkDepot> weekOrders = new ArrayList<NetworkDepot>();
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        NetworkCandidate candidate;/*
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            candidate = new NetworkCandidate();
            candidate.setCandidatePoint(getStringCellValue7(sheet.getRow(i), 0));
            candidate.setType(getIntCellValue7(sheet.getRow(i), 1));
            weekOrders.add(candidate);
        }*/
        return weekOrders;
    }
}
